/**
 * BaseConverter.c - Convert decimal to any base, and any base to decimal
 *					 numbers. Only for positive integers, and bases between
 *					 2 and 36.
 */
#include<stdio.h>
#include<string.h>
#include<math.h> 

char digits[] = {'0','1', '2','3','4','5','6','7','8','9','A','B','C','D',
                 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
                 'S','T','U','V','W','X','Y','Z'};
					
char * decimalToAny(int number, int base)
{
   	int index;
  	int digitValue;
   	char *result = new char[40];
   	int counter = 0;
	
   	if((number > 0) && (base >= 2 && base <= 36))
    	while(number) {
        	digitValue = number % base;
            number /= base;
            result[counter++] = digits[digitValue];
        }
	result[strlen(result)] = '\0';
	return result;
}

int anyToDecimal(char * number, int base)
{
	int n = strlen(number);
	int index; 
	int counter = 0;
	int result = 0;
	for(index = n-1; index>=0; index--)  {
		result += (number[index]>='A')?(number[index]-'A'+10):(number[index]-'0')*(int)pow(base,counter++);
	}
	return result;
}

int main() {
	char * ea = decimalToAny(255, 2);
	printf("%s\n", ea);
	printf("%d\n", anyToDecimal(ea, 2)); 
	return 0;
}
